﻿<template>
  <div class="table">
    <!--表2季度生产情况T_MulQuartProduc-->
    <div class="divSearch">
      <Row>
        <Col span="24" class="col-margin"> 填报年度：
        <Select v-model="searchModel.FillInYear" placeholder="全部" style="width: 100px" @on-change="getReportData()">
          <Option value="">全部</Option>
          <Option :value="item.DictText" v-for="item in CreateYearList" :key="item.DictValue">{{item.DictText}}</Option>
        </Select>
        <Button icon="ios-search" type="ghost" @click="getReportData()" class="ButtonStyle">
          查询
        </Button>
        <Button icon="ios-download" type="primary" @click="ExcelDown()" class="searchButton" style="float:right;width: 100px;">数据导出</Button>
        <Upload :on-success="handleSuccess" :action="GetPostUrl()" style="float:right;width: 100px;" v-show="ReportStatus <3">
          <Button type="primary" icon="ios-cloud-upload-outline" class="searchButton">数据导入</Button>
        </Upload>
        <Button icon="ios-cloud-download" type="primary" @click="TmpDown()" class="searchButton" style="width: 100px;">模板下载</Button>
        <!-- <label style="float:right;padding: 6px;font-size:16px;">当前状态:
          <span v-if="ReportStatus==0" class="Error"></span> 
          <span v-else-if="ReportStatus==1" class="NoReport">未填写</span>
          <span v-else-if="ReportStatus==2" class="AlreadyReport">已填写</span>
          <span v-else-if="ReportStatus==3" class="UpReport">已上报</span>
          <span v-else-if="ReportStatus==4" class="Success">审核通过</span>                           
      </label> -->
        </Col>
      </Row>
    </div>
    <!-- <Row style="padding-left: 10px;">
      <Col>
      <Button icon="plus" type="ghost" size="small" @click="addItem" :disabled="ReportStatus < 3 && BtnSaveStatus ? false:true">新增</Button>
      </Col>
    </Row> -->
    <Row>
      <Col>
      <table class="reptable" >
        <caption>
            <label style="float:left">{{searchModel.FillInYear}}表9养蚕收益情况填报(抽样统计)</label>
            <label style="float:right">当前状态:
              <Poptip trigger="hover" placement="left-start" v-model="visible" :content="ReportOpinion" v-if="ReportStatus==0">
                  <span  class="Error">重新填写</span> 
                </Poptip>
              <!-- <span v-if="ReportStatus==0" class="Error">重新填写（{{Opinion}}）</span>  -->
              <span v-else-if="ReportStatus==1" class="NoReport">未填写</span>
              <span v-else-if="ReportStatus==2" class="AlreadyReport">已填写</span>
              <span v-else-if="ReportStatus==3" class="UpReport">已上报</span>
              <span v-else-if="ReportStatus==4" class="Success">审核通过</span>     
              <span v-else class="NoReport">无需填报</span>              
            </label><br/>
             <label style="float:left;margin-bottom:5px">
               <Button icon="plus" type="success" size="small" @click="addItem" :disabled="ReportStatus < 3 && BtnSaveStatus ? false:true">新增</Button>
            </label> 
        </caption>
      </table>
      <div style="width:100%; overflow:scroll;">
        <table class="ContentTable"  style="width:150%;">
          <thead>
            <tr>
              <th rowspan="2">序号</th>
              <th rowspan="2">操作</th>
              <th rowspan="2">农户<br/>姓名</th>
              <th rowspan="2">桑地<br/>类型</th>
              <th rowspan="2">树龄</th>
              <th rowspan="2">种植<br/>规格</th>
              <th rowspan="2">桑园<br/>海拔</th>
              <th rowspan="2">剪伐<br/>方式</th>
              <th rowspan="2">桑园面积(亩)</th>
              <th colspan="5">养蚕量(张)</th>
              <th colspan="5">鲜茧产量(kg)</th>
              <th colspan="5">鲜茧价格(元/kg)</th>
              <th colspan="2">蚕沙</th>
              <th rowspan="2">平均亩桑效益（元/亩）</th>
              <th rowspan="2">平均张种效益（元/张）</th>
            </tr>
            <tr>
              <th>合计</th>
              <th>春</th>
              <th>夏</th>
              <th>正秋</th>
              <th>晚秋</th>
              <th>合计</th>
              <th>春</th>
              <th>夏</th>
              <th>正秋</th>
              <th>晚秋</th>
              <th>均价</th>
              <th>春</th>
              <th>夏</th>
              <th>正秋</th>
              <th>晚秋</th>
              <th>产量<br/>(公斤)</th>
              <th>价格<br/>(元/公斤)</th>
            </tr>
          </thead>
          <tbody style="max-height:220px;">
            <tr v-for="(dataitem,index) in datalist" :key="dataitem.Id">
              <td>{{index+1}}</td>
              <td>
                <Button type="ghost" size="small" icon="ios-trash-outline" style="color:#FF0000" @click="delOp(dataitem)" :disabled="ReportStatus < 3 && BtnSaveStatus ? false:true">
                </Button>
              </td>
              <td>
                <Input v-model="dataitem.HouseName"></Input>
              </td>
              <td>
                <Input style="display:none;" v-model="dataitem.Id"></Input>
                <Input style="display:none;" v-model="dataitem.ReportId"></Input>
                <Select v-model="dataitem.SanType">
                  <Option :value="item.DictValue" v-for="item in MulLandList" :key="item.DictValue">{{item.DictText}}</Option>
                </Select>
              </td>
              <td>
                <Input v-model="dataitem.Age"></Input>
              </td>
              <td>
                <Input v-model="dataitem.PlantSanc"></Input>
              </td>
              <td>
                <Input v-model="dataitem.Elevation"></Input>
              </td>
              <td>
                <Select v-model="dataitem.CutMethod">
                  <Option :value="item.DictValue" v-for="item in CutMethodList" :key="item.DictValue">{{item.DictText}}</Option>
                </Select>
              </td>
              <td>
                <Input v-model="dataitem.MulArea"></Input>
              </td>
              <td>
                {{Number(dataitem.SilkwormSpring)+Number(dataitem.SilkwormSummer)+Number(dataitem.SilkwormIsAutumn)+Number(dataitem.SilkwormLateAutumn)}}
              </td>
              <td>
                <Input v-model="dataitem.SilkwormSpring"></Input>
              </td>
              <td>
                <Input v-model="dataitem.SilkwormSummer"></Input>
              </td>
              <td>
                <Input v-model="dataitem.SilkwormIsAutumn"></Input>
              </td>
              <td>
                <Input v-model="dataitem.SilkwormLateAutumn"></Input>
              </td>
              <td>
                {{Number(dataitem.FreshSpring)+Number(dataitem.FreshSummer)+Number(dataitem.FreshIsAutumn)+Number(dataitem.FreshLateAutumn)|decimal}}
              </td>
              <td>
                <Input v-model="dataitem.FreshSpring"></Input>
              </td>
              <td>
                <Input v-model="dataitem.FreshSummer"></Input>
              </td>
              <td>
                <Input v-model="dataitem.FreshIsAutumn"></Input>
              </td>
              <td>
                <Input v-model="dataitem.FreshLateAutumn"></Input>
              </td>
              <td>
                {{((Number(dataitem.PriceSpring)*Number(dataitem.FreshSpring))+(Number(dataitem.PriceSummer)*Number(dataitem.FreshSummer))
                +(Number(dataitem.PriceIsAutumn)*Number(dataitem.FreshIsAutumn))+(Number(dataitem.PriceLateAutumn)*Number(dataitem.FreshLateAutumn)))/
                (Number(dataitem.FreshSpring)+Number(dataitem.FreshSummer)+Number(dataitem.FreshIsAutumn)+Number(dataitem.FreshLateAutumn))|decimal}}
              </td>
              <td>
                <Input v-model="dataitem.PriceSpring"></Input>
              </td>
              <td>
                <Input v-model="dataitem.PriceSummer"></Input>
              </td>
              <td>
                <Input v-model="dataitem.PriceIsAutumn"></Input>
              </td>
              <td>
                <Input v-model="dataitem.PriceLateAutumn"></Input>
              </td>
              <td>
                <Input v-model="dataitem.SilkwormYield"></Input>
              </td>
              <td>
                <Input v-model="dataitem.SilkwormPrice"></Input>
              </td>
              <td>
                {{(((Number(dataitem.PriceSpring)*Number(dataitem.FreshSpring))+(Number(dataitem.PriceSummer)*Number(dataitem.FreshSummer))
                +(Number(dataitem.PriceIsAutumn)*Number(dataitem.FreshIsAutumn))+(Number(dataitem.PriceLateAutumn)*Number(dataitem.FreshLateAutumn)))
                +(Number(dataitem.SilkwormYield)*Number(dataitem.SilkwormPrice)))/Number(dataitem.MulArea)|decimal}}
              </td>
              <td>
                {{(((Number(dataitem.PriceSpring)*Number(dataitem.FreshSpring))+(Number(dataitem.PriceSummer)*Number(dataitem.FreshSummer))
                +(Number(dataitem.PriceIsAutumn)*Number(dataitem.FreshIsAutumn))+(Number(dataitem.PriceLateAutumn)*Number(dataitem.FreshLateAutumn)))
                +(Number(dataitem.SilkwormYield)*Number(dataitem.SilkwormPrice)))/(Number(dataitem.SilkwormSpring)+Number(dataitem.SilkwormSummer)+Number(dataitem.SilkwormIsAutumn)+Number(dataitem.SilkwormLateAutumn))|decimal}}
              </td>
            </tr>
          </tbody>
        </table>
      </div>
      <div  class="divButton">
        <Button type="primary" @click="save()" :disabled="ReportStatus < 3 && BtnSaveStatus ? false:true">保存</Button>
        <Button type="primary" @click="DataUp()" :disabled="ReportStatus == 2 ? false:true">上报</Button>
      </div>
      <Alert banner type="error">
        <p style="color:red;">填报说明:</p>
        <p>1、“桑地类型”、“剪伐方式”请在选择框内进行选择；</p>
        <p>2、种植规格指桑树的株行距，即行距X株距，单位为m；</p>
        <p>3、桑园面积指调查对象所拥有的桑园总面积；</p>
        <p>4、蚕沙产量指售出的蚕沙重量，若没有售出，请填0；</p>
        <p>5、养蚕收益调查对象是正常养蚕的农户，每年亩均养蚕量低于1张，请不要填入；</p>
        <p style="color:red;font-size: 16px;padding-top: 10px;">特别注意：若指标项没有数据，请直接填“0”</p>
        <span slot="desc"></span>
      </Alert>
      </Col>
    </Row>
  </div>
</template>

<script>
export default {
  data() {
    const validateNumber = (rule, value, callback) => {
      if (!Number.isInteger(value)) {
        callback(new Error('请输入数字值'));
      } else {
        if (value < 18) {
          callback(new Error('必须年满18岁'));
        } else {
          callback();
        }
      }
    };
    return {
      visible:true,
      ReportOpinion:'',
      datalist: [],
      datas: {}, //table数据对象
      dataIds: [],
      index:0,
      ReportOpinion:'',
      CutMethodList: this.GetGroupDict("LoggingType"),
      MulLandList: this.GetGroupDict("MulLandType"),
      CreateYearList: this.GetGroupDict("ReportYear"), //上报年份
      CurrentYear: this.GetCurrentYear(), //当前年度
      ReportId: '',
      ReportStatus: '',
      BtnSaveStatus:false,
      //查询模型：根据需求删除不不要查询条件
      searchModel: {
        ReportCode: 'MulSerEarnSurvey',
        FillInYear: '',
        TimesType: '',
        AreaCode: this.GetlocaStorage("CSuserinfo").AreaCode,
      },
      clientHeight: window.innerHeight - 260, //表格默认高度
    }
  },
  mounted() {
    //table高度计算方法
    const that = this;
    window.onresize = () => {
      return (() => {
        that.clientHeight = window.innerHeight - 260;
      })()
    };
  },
  methods: {
    addItem() {
      var model = { //编辑模型
        Id: 0,
        ReportId: '',
        HouseName: '',
        SanType: '',
        Age: '',
        PlantSanc: '',
        Elevation: '',
        CutMethod: '',
        MulArea: '',
        total1:'',
        SilkwormSpring: '',
        SilkwormSummer: '',
        SilkwormIsAutumn: '',
        SilkwormLateAutumn: '',
        FreshSpring: '',
        FreshSummer: '',
        FreshIsAutumn: '',
        FreshLateAutumn: '',
        PriceSpring: '',
        PriceSummer: '',
        PriceIsAutumn: '',
        PriceLateAutumn: '',
        SilkwormYield: '',
        SilkwormPrice: '',
        AverageBenefit: '',
      };
      model.ReportId = this.ReportId;
      model.Id = this.datalist.length + 1;
      this.datalist.push(model);
    },
    delOp(m) {
      for (var i = 0; i < this.datalist.length; i++) {
        if (this.datalist[i].Id == m.Id) {
          this.datalist.splice(i, 1);
        }
      }
    },
    //数据上报
      DataUp() {
        var self = this;
        self.$Modal.confirm({
          title: '提示信息',
          content: '<p>数据上报后，不能再更改，是否继续上报？</p>',
          onOk: () => {
            var model = new Object();
            model.Id = self.ReportId;
            model.reportType = '3';
            model.strMsg = '';
            this.$http.post("Report/PostUpStatus", model).then(
              function (res) {
                if (res.data.status == 1) {
                  self.$Notice.success({
                    title: '数据上报成功！',
                    desc: false
                  });
                  self.getReportData();
                } else {
                  self.$Modal.error({
                    title: "提示信息",
                    content: "数据上报失败!"
                  });
                }
              }
            );
          }
        });
      },
      getReportData() {
        var self = this;
        var user = this.GetlocaStorage("CSuserinfo");
        this.searchModel.AreaCode = user.AreaCode;
        if (self.searchModel.FillInYear != null && self.searchModel.FillInYear != '') {
          this.searchModel.FillInYear = self.searchModel.FillInYear;
        } else {
          this.searchModel.FillInYear = self.CurrentYear + '年'; //填报年度-----数据字典值
        }
        self.$http.post('Report/PostPageList', this.searchModel).then(function (res) {
          if (res.data.status === 1) {
            if (res.data.rows.length > 0) {
              self.ReportId = res.data.rows[0].Id;
              self.ReportStatus = res.data.rows[0].Status;
              self.BtnSaveStatus = true;
            } else {
              self.ReportId = '';
              self.ReportStatus = '';
              self.BtnSaveStatus = false;
            }
            self.getPageData(self.ReportId);
          } else {
            self.$Modal.error({
              title: '提示信息',
              content: res.data.message
            });
          }
        });
      },
      //获取this.datas: {},//table数据对象
      getPageData(ReportId) {
        var self = this;
        var reportCode = self.searchModel.ReportCode;
        this.$http.get("Report", {
          params: {
            id: ReportId,
            reportCode: reportCode
          }
        }).then( //获取数据编辑项的值
          function (res) {
            switch (res.data.list.status) {
              case 1:
                self.ReportOpinion=res.data.Opinion;
                self.datalist = res.data.list.model;
                break;
              case 0:
                self.$Modal.error({
                  title: "提示信息",
                  content: "数据加载失败!!!"
                });
                break;
              case -1:
                self.$Modal.error({
                  title: "提示信息",
                  content: "数据加载失败!!!"
                });
                break;
            }
          }
        );
      },
      //检查整数
      checkInt(value){
        var reg = /\S/;
        var temp = /^[0-9]\d*(\.\d+)?$/;//数字验证
        var p=/^[0-9]+([.]{1}[0-9]{0})?$/;//匹配整数或带一个小数
        if (reg.test(value)) {
          if (temp.test(value) == false) 
          {
            return false;
          }
          else {
            if(p.test(value)==false)
            {
              return false;
            }
            else{
              return true;
            }
          }
        } 
        else {
          return false;
        }
      },
      //检查一位小数
      checkOne(value){
        var reg = /\S/;
        var temp = /^[0-9]\d*(\.\d+)?$/;//数字验证
        var p=/^[0-9]+([.]{1}[0-9]{1})?$/;//匹配整数或带一个小数
        if (reg.test(value)) {
          if (temp.test(value) == false) 
          {
            return false;
          }
          else {
            if(p.test(value)==false)
            {
              return false;
            }
            else{
              return true;
            }
          }
        } 
        else {
          return false;
        }
      },
      //检查空
      checkName(value){
        var reg = /\S/;
        if (reg.test(value)&&value!=null) {
          return true;
        } 
        else {
          return false;
        }
      },
       //保存方法
      save() {
        var self = this;
        var m=0;
          for(var i=0;i<self.datalist.length;i++)
          {
            if(self.checkName(self.datalist[i].HouseName)&&self.checkName(self.datalist[i].SanType)
            &&self.checkOne(self.datalist[i].Age)&&self.checkName(self.datalist[i].PlantSanc)
            &&self.checkName(self.datalist[i].CutMethod)&&self.checkName(self.datalist[i].PlantSanc)
            &&self.checkOne(self.datalist[i].MulArea)&&self.checkInt(self.datalist[i].SilkwormSpring)
            &&self.checkInt(self.datalist[i].SilkwormSummer)&&self.checkInt(self.datalist[i].SilkwormIsAutumn)
            &&self.checkOne(self.datalist[i].SilkwormLateAutumn)&&self.checkOne(self.datalist[i].FreshSpring)
            &&self.checkOne(self.datalist[i].FreshSummer)&&self.checkOne(self.datalist[i].FreshIsAutumn)
            &&self.checkOne(self.datalist[i].FreshLateAutumn)&&self.checkOne(self.datalist[i].PriceSpring)
            &&self.checkOne(self.datalist[i].PriceSummer)&&self.checkOne(self.datalist[i].PriceIsAutumn)
            &&self.checkOne(self.datalist[i].PriceLateAutumn)&&self.checkOne(self.datalist[i].SilkwormYield)
            &&self.checkOne(self.datalist[i].SilkwormPrice))
            {
              m+=1;
            }
          }
          if( m==self.datalist.length)
          {
             this.$http.post('MulSerEarnSurvey', self.datalist).then(
              function (res) {
                if (res.data.status == 1) {
                  self.getReportData();
                  self.$Notice.success({
                    title: '保存成功!',
                    desc: false
                  });
                } else {
                  self.$Modal.warning({
                    title: "提示信息",
                    content: "数据保存失败,请与管理员联系!"
                  });
                }
              });
          }else{
            this.$Notice.error({title: '数据有误,请检查后再试!',desc: '',});
        }
      },
      //导入
      GetPostUrl() {
        var url = this.$http.defaults.baseURL + "Report/PostImportData?FillInYear="+this.searchModel.FillInYear+"&&ReportCode=" + this.searchModel.ReportCode +
          "&&AreaCode=" + this.searchModel.AreaCode;
        return url;
      },
      handleSuccess(res, file) {
        var self = this;
        self.getReportData();
        self.$Notice.success({
          title: '数据导入成功！',
          desc: false
        });
      },
      //导出
      ExcelDown: function () {
        var self = this;
        var model = new Object();
        model.ReportCode = self.searchModel.ReportCode;
        if (self.searchModel.FillInYear != null && self.searchModel.FillInYear != '') {
          model.FillInYear = self.searchModel.FillInYear;
        } else {
          model.FillInYear = self.CurrentYear + '年'; //填报年度-----数据字典值
          self.searchModel.FillInYear = self.CurrentYear + '年';
        }
        model.AreaCode = self.searchModel.AreaCode;
        window.open(this.$http.defaults.baseURL + "MulSerEarnSurvey/GetExcelData?ReportCode=" + model.ReportCode +
          "&&FillInYear=" + model.FillInYear + "&&AreaCode=" + model.AreaCode);
      },
      //模板下载
      TmpDown: function () {
        window.open(this.$http.defaults.baseURL + "Report/GetTemplateData?ReportCode=" + this.searchModel.ReportCode);
      },
  },
  //创建Vue页面方法
  created: function () {
    //创业Vue前调用获取this.datas: {},//table数据对象
    this.getReportData();
  },
  watch: {
    //需要监听的在此处新增监听对象
  }
}
</script>
<style scoped src="../../../styles/stats.css"></style>